package com.erb.controller.hzg;

import com.erb.constants.AdminInformConstants;
import com.erb.constants.LoanConstants;
import com.erb.entity.*;
import com.erb.service.hzg.LoanMakeService;
import com.erb.service.hzg.LoanUserService;
import com.erb.service.zwj.IApplyService;
import com.erb.service.zwj.IInformService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/loan/back-make")
public class Loanmake {
	@Resource
	LoanMakeService service;
	@Resource
	private LoanUserService loanUserService;
	@Resource
	private IInformService informService;
	@Resource
	private IApplyService applyService;

	@RequestMapping("/makelist")
	public String makelist(Pager pager, Model model) {
		pager.page(service.selProcount());
		pager.data = service.selApplyPage(pager);
		model.addAttribute("pager", pager);

		List<Map> mapList = service.makelist();
		model.addAttribute("makelist", pager.data);
		return "loan/loanmake";
	}

	@RequestMapping("/makeupd")
	@ResponseBody
	public String makeupd(int id, String maxmoney, HttpSession session) {
		System.out.print("传入信息: " + maxmoney);
		System.out.println("id" + id);
		int i = service.auditupd(id, maxmoney, LoanConstants.SMLOANSTATE_AGREE);
		System.out.println("影响行数为 ：" + i);

		LoanUser loanUser = loanUserService.lulist(id);
		Apply applyById = applyService.findApplyById(id);

		// 保存成功后，向用户发送通知
		Inform inform = new Inform();
		inform.setSendTime(System.currentTimeMillis());
		inform.setInformSenderId(AdminInformConstants.ADMIN_ID_LABEL);
		inform.setInformRecipientsId(applyById.getUid());
		inform.setInformTitle("贷款审核");
		inform.setInformContent("尊敬的用户你好，你的贷款申请管理员已审核完毕。快去贷款吧~");
		informService.saveInform(inform);

		return "ok";
	}

	//评估显示金额
	@RequestMapping("/tolimit")
	public String tolimit(Pager pager, Model model) {
		//分页
		pager.page(service.sellimProcount());
		pager.data = service.selApplylimPage(pager);
		model.addAttribute("pager", pager);

		model.addAttribute("limitlist", pager.data);
		return "loan/loanLimit";
	}

	@RequestMapping("/selmodalapply")
	public String selmodalapply(int id, String applyMType, Model model) {
		System.out.println("用户id ：" + id);
		if (applyMType.equals("房产")) {
			List<Houseimf> houseimfs = service.Houselist(id);
			model.addAttribute("houseimfs", houseimfs);
			return "loan/loanShowhouseApply";
		} else if (applyMType.equals("车辆")) {
			List<Carimf> carmap = service.idshowlist(id);
			model.addAttribute("carmap", carmap);
			return "loan/loanShowApply";
		}
		return "loan/loanShowallApply";
	}

	@RequestMapping("/updlimit")
	@ResponseBody
	public String updlimit(int id, String limitmoney) {
		System.out.println("id: " + id + "limitmoney :" + limitmoney);
		int i = service.updlimit(id, limitmoney, LoanConstants.Evaluation_Of_State_True);
		if (i > 0) {
			return "成功";
		}
		return "失败";
	}


}
